//
//  MSSearchBar.m
//  MyShare
//
//  Created by koki on 15/9/18.
//  Copyright © 2015年 MyShare. All rights reserved.
//

#import "MSSearchBar.h"
#import "MSMainViewController.h"

@interface MSSearchBar ()
@property CGPoint anchorPoint;

@end

@implementation MSSearchBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.anchorPoint=frame.origin;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.hasCentredPlaceholder = YES;
        self.delegate = (id)self;
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    UIView *backgroundView = [((UIView *)[self.subviews objectAtIndex:0]).subviews firstObject];
    backgroundView.hidden=YES;
    
    UITextField *dySearchField=[((UIView *)[self.subviews objectAtIndex:0]).subviews lastObject];
    dySearchField.frame=CGRectInset(dySearchField.frame, 2, 4);
    dySearchField.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13];
    dySearchField.top-=2;
    dySearchField.clearButtonMode = self.clearButtonMode;
    if (self.placeholderColor) {
        [dySearchField setValue:self.placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
    }
    
    if (self.leftView) {
        dySearchField.leftView = self.leftView;
    }
    if (self.rightView) {
        dySearchField.rightViewMode = UITextFieldViewModeAlways;
        dySearchField.rightView = self.rightView;
    } else {
        dySearchField.rightViewMode = UITextFieldViewModeNever;
    }
    if (self.textColor) {
        dySearchField.textColor = self.textColor;
    }
    if (self.customColor) {
        dySearchField.backgroundColor=self.customColor;
    } else {
        dySearchField.backgroundColor=[UIColor whiteColor];
    }
    if (self.borderColor) {
        dySearchField.layer.borderColor=self.borderColor.CGColor;
        dySearchField.layer.borderWidth=0.5;
    }
    dySearchField.height=self.height-10;
    dySearchField.layer.cornerRadius=self.cornerRadius?self.cornerRadius:3;//dySearchField.height/2.0;
    
    //布局锚点范围内的offset
    if (self.anchorScrollView) {
        CGFloat offsetY = _anchorScrollView.contentOffset.y;
        if (offsetY > self.anchorPoint.y-20)
        {
            self.top=offsetY+20;
        }
        else
        {
            self.top=self.anchorPoint.y;
        }
    }
}

-(void)removeObserver
{
    [_anchorScrollView removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)setAnchorScrollView:(UIScrollView *)anchorScrollView
{
    [_anchorScrollView removeObserver:self forKeyPath:@"contentOffset"];
    _anchorScrollView = anchorScrollView;
    _anchorScrollView.scrollsToTop = NO;
    [_anchorScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder
{
    _hasCentredPlaceholder = hasCentredPlaceholder;
    SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
    if ([self respondsToSelector:centerSelector])
    {
        NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setTarget:self];
        [invocation setSelector:centerSelector];
        [invocation setArgument:&_hasCentredPlaceholder atIndex:2];
        [invocation invoke];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [self resignFirstResponder];
    [self setNeedsLayout];
}

#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{

    return NO;
}

@end








